package com.zxp.utils.fileUtils;

import com.zxp.utils.configParseUtils.JsonConfigReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class to retrieve a list of subdirectories in a specified directory.
 */
public class DirectoryListReader {

    /**
     * Retrieves a list of subdirectory names in the specified directory.
     *
     * @param directoryPath The path of the directory to list subdirectories from.
     * @return A list of subdirectory names.
     */
    public static List<String> listDirectories(String directoryPath) {
        List<String> directoryList = new ArrayList<>();
        File directory = new File(directoryPath);

        // Check if the specified path is a valid directory
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();

            // Traverse the files in the directory
            if (files != null) {
                for (File file : files) {
                    // Check if the file is a directory
                    if (file.isDirectory()) {
                        directoryList.add(file.getName());
                    }
                }
            }
        } else {
            System.err.println("The specified path is not a valid directory");
        }

        return directoryList;
    }

    /**
     * Retrieves the current working directory.
     *
     * @return The current working directory.
     */
    public static String getCurrentDirectory() {
        return System.getProperty("user.dir");
    }

    /**
     * Main method for testing the DirectoryListReader class.
     *
     * @param args Command-line arguments (not used in this example).
     */
    public static void main(String[] args) {
        String directoryPath = JsonConfigReader.getString("projectPath");
        // Get the list of subdirectories
        List<String> directoryList = listDirectories(directoryPath);

        // Print the list of subdirectories
        System.out.println("List of subdirectories in the directory:");
        for (String directoryName : directoryList) {
            System.out.println(directoryName);
        }
        // Example usage:
        String currentDirectory = getCurrentDirectory();
        System.out.println("Current Directory: " + currentDirectory);
    }
}
